package wowmobileclient;

import java.util.Vector;

public class OnlineUsers {

    private Canvas canvas;
    private Vector user_list = new Vector();

    public OnlineUsers(Canvas canvas) {
        this.canvas = canvas;
    }

    public void remove(int id) {
        user_list.removeElementAt(id);
    }

    public void addFromString(String p) {
        eraseItems();
        String[] players = SplitString.split(p, "[:]");
        for (int i = 0; i < players.length; ++i) {
            String[] p_fields = SplitString.split(players[i], ",");
            Player player = new Player(Integer.parseInt(p_fields[0]), Integer.parseInt(p_fields[1]), Integer.parseInt(p_fields[2]));
            user_list.addElement(player);
        }
        refreshItems();
    }

    public int add(Player o) {
        int id = user_list.size();
        user_list.insertElementAt(o, id);
        return id;
    }

    public Player get(int id) {
        return (Player) user_list.elementAt(id);
    }

    public void eraseItems() {
        //remove from old positions
        canvas.users_array = new int[canvas.gameX][canvas.gameY];
        user_list.removeAllElements();
    }

    public void refreshItems() {
        for (int i = 0; i < user_list.size(); ++i) {
            Player el = (Player) user_list.elementAt(i);
            canvas.users_array[el.x][el.y] = el.type;
        }
    }
}
